//
//  PopupView.m
//  MartketNote
//
//  Created by giangnm on 1/23/14.
//  Copyright (c) 2014 Nguyen Minh Giang. All rights reserved.
//

#import "PopupView.h"

@implementation MaskView
@synthesize delegate = _delegate;

-(id)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        //mask
        UIView *mask = [[UIView alloc] initWithFrame:frame];
        mask.backgroundColor = [UIColor blackColor];
        mask.alpha = 0.4;
        [self addSubview:mask];
    }
    return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//    NSLog(@"Touch mask!");
    [_delegate touchMaskViewDelegate];
}

@end


@implementation PopupView
@synthesize delegate = _delegate;

- (id)initWithFrame:(CGRect)frame title:(NSString *)title message:(NSString *)message titleButtonArray:(NSArray *)titleArray
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        CGSize size = [[UIScreen mainScreen] bounds].size;
        
        //mask
        MaskView *maskView = [[MaskView alloc] initWithFrame:frame];
        maskView.delegate = self;
        [self addSubview:maskView];
        
        //popup global view
        UIView *popupView = [[UIView alloc] initWithFrame:CGRectMake(30, (size.height - HEIGHT_POPUP_0)/2, size.width - 60, HEIGHT_POPUP_0)];
        [popupView.layer setCornerRadius:5.0];
        [popupView.layer setMasksToBounds:YES];
        popupView.backgroundColor = [UIColor whiteColor];
        popupView.alpha = 0.9;
        [self addSubview:popupView];
        
        //popup view
        UIView *bgPopupView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, popupView.frame.size.width, popupView.frame.size.height)];
        bgPopupView.backgroundColor = [UIColor whiteColor];
        [bgPopupView.layer setCornerRadius:5.0];
        [bgPopupView.layer setMasksToBounds:YES];
        bgPopupView.alpha = 0.9;
        [popupView addSubview:bgPopupView];
        
        //title
        UILabel *lbTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 12, popupView.frame.size.width, 30)];
        lbTitle.text = title;
        lbTitle.textColor = [UIColor blackColor];
        lbTitle.textAlignment = NSTextAlignmentCenter;
        lbTitle.font = [UIFont boldSystemFontOfSize:16];
        lbTitle.backgroundColor = [UIColor clearColor];
        [popupView addSubview:lbTitle];
        
        
        int count = [titleArray count];
        
        //message
        UITextView *tvMessage = [[UITextView alloc] initWithFrame:CGRectMake(15, lbTitle.frame.origin.y + lbTitle.frame.size.height + 5, popupView.frame.size.width - 30, popupView.frame.size.height - (lbTitle.frame.origin.y + lbTitle.frame.size.height + 5) - HEIGHT_BUTTON*count)];
        tvMessage.text = message;
        tvMessage.textAlignment = NSTextAlignmentCenter;
        tvMessage.textColor = [UIColor blackColor];
        tvMessage.font = [UIFont systemFontOfSize:16];
        [tvMessage setUserInteractionEnabled:NO];
        tvMessage.backgroundColor = [UIColor clearColor];
        [popupView addSubview:tvMessage];
        
        //button list
        for(int i=0; i<count; i++)
        {
            //btn
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(-10, popupView.frame.size.height - HEIGHT_BUTTON*(count-i), popupView.frame.size.width + 20, HEIGHT_BUTTON);
            [btn setTitle:[titleArray objectAtIndex:i] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor colorWithRed:0.0f green:0.48f blue:1.0f alpha:1.0f] forState:UIControlStateNormal];
            btn.backgroundColor = [UIColor clearColor];
            btn.titleLabel.font = [UIFont systemFontOfSize:16];
            [btn addTarget:self action:@selector(buttonTouchUpOutSide:) forControlEvents:UIControlEventTouchCancel|UIControlEventTouchDragOutside];
            [btn addTarget:self action:@selector(buttonTouchUpInSide:) forControlEvents:UIControlEventTouchDown];
            [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            if(i == count - 1)
                btn.tag = TAG_BUTTON_CLOSE;
            else
                btn.tag = TAG_BUTTON_BEGIN + i;
            [popupView addSubview:btn];
            
            //line top button
            UIView *lineBtn = [[UIView alloc] initWithFrame:CGRectMake(0, 0, btn.frame.size.width, 1.0f)];
            lineBtn.backgroundColor = [UIColor grayColor];
            [btn addSubview:lineBtn];
        }
        
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame title:(NSString *)title message:(NSString *)message
{
    if(self = [super initWithFrame:frame])
    {
        CGSize size = [[UIScreen mainScreen] bounds].size;
        
        //mask
        MaskView *maskView = [[MaskView alloc] initWithFrame:frame];
        maskView.delegate = self;
        [self addSubview:maskView];
        
        //popup global view
        UIView *popupView = [[UIView alloc] initWithFrame:CGRectMake(30, (size.height - HEIGHT_POPUP_1)/2, size.width - 60, HEIGHT_POPUP_1)];
        [popupView.layer setCornerRadius:5.0];
        [popupView.layer setMasksToBounds:YES];
        popupView.backgroundColor = [UIColor whiteColor];
        popupView.alpha = 0.9;
        [self addSubview:popupView];
        
        //popup view
        UIView *bgPopupView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, popupView.frame.size.width, popupView.frame.size.height)];
        bgPopupView.backgroundColor = [UIColor whiteColor];
        [bgPopupView.layer setCornerRadius:5.0];
        [bgPopupView.layer setMasksToBounds:YES];
        bgPopupView.alpha = 0.9;
        [popupView addSubview:bgPopupView];
        
        //title
        UILabel *lbTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 12, popupView.frame.size.width, 30)];
        lbTitle.text = title;
        lbTitle.textColor = [UIColor blackColor];
        lbTitle.textAlignment = NSTextAlignmentCenter;
        lbTitle.font = [UIFont boldSystemFontOfSize:16];
        lbTitle.backgroundColor = [UIColor clearColor];
        [popupView addSubview:lbTitle];
        
        //message
        UITextView *tvMessage = [[UITextView alloc] initWithFrame:CGRectMake(15, lbTitle.frame.origin.y + lbTitle.frame.size.height + 5, popupView.frame.size.width - 30, popupView.frame.size.height - (lbTitle.frame.origin.y + lbTitle.frame.size.height + 5) - HEIGHT_BUTTON)];
        tvMessage.text = message;
        tvMessage.textAlignment = NSTextAlignmentCenter;
        tvMessage.textColor = [UIColor blackColor];
        tvMessage.font = [UIFont systemFontOfSize:16];
        [tvMessage setUserInteractionEnabled:NO];
        tvMessage.backgroundColor = [UIColor clearColor];
        [popupView addSubview:tvMessage];
        
        //btn
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(-10, popupView.frame.size.height - HEIGHT_BUTTON, popupView.frame.size.width + 20, HEIGHT_BUTTON);
        [btn setTitle:[LanguageClass getStringWithLanguge:[ReadWriteDataClass getLanguage] withKey:KEY_CLOSE_BUTTON] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithRed:0.0f green:0.48f blue:1.0f alpha:1.0f] forState:UIControlStateNormal];
        btn.backgroundColor = [UIColor clearColor];
        btn.titleLabel.font = [UIFont systemFontOfSize:16];
        [btn addTarget:self action:@selector(buttonTouchUpOutSide:) forControlEvents:UIControlEventTouchCancel|UIControlEventTouchDragOutside];
        [btn addTarget:self action:@selector(buttonTouchUpInSide:) forControlEvents:UIControlEventTouchDown];
        [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = TAG_BUTTON_CLOSE;
        [popupView addSubview:btn];
        
        //line top button
        UIView *lineBtn = [[UIView alloc] initWithFrame:CGRectMake(0, 0, btn.frame.size.width, 1.0f)];
        lineBtn.backgroundColor = [UIColor grayColor];
        [btn addSubview:lineBtn];
    }
    return self;
}

-(void)buttonTouchUpOutSide:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    btn.backgroundColor = [UIColor clearColor];
}

-(void)buttonTouchUpInSide:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    btn.backgroundColor = [UIColor lightGrayColor];
}

-(void)buttonClick:(id)sender
{
    [self buttonTouchUpOutSide:sender];
    if([sender tag] != TAG_BUTTON_CLOSE)
        [_delegate popupViewEventButtonDelegate:sender];
    [self setHidden:YES];
}

-(void)touchMaskViewDelegate
{
//    NSLog(@"Touch delegate");
    [self setHidden:YES];
}

@end
